package demo.annotation;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author GuoFei
 * @date 2018/9/12 14:59
 */
@Aspect
@Component
public class AnnotationHelper {

    private static ThreadLocal<Boolean> myAnnotationLocal = new ThreadLocal<Boolean>();

    public static boolean canContinue(){
        Boolean r = myAnnotationLocal.get();
        return r == null?false:r;
    }


    @Before(value = "pointCut() && @annotation(myAnnotation)")
    public void before(MyAnnotation myAnnotation) {
        System.out.println("set_myAnnotation:"+myAnnotation.canContinue());
        myAnnotationLocal.set(myAnnotation.canContinue());
    }

    @After(value = "pointCut() && @annotation(myAnnotation)")
    public void after(MyAnnotation myAnnotation) {
        System.out.println("remove_myAnnotation:"+myAnnotation.canContinue());
        myAnnotationLocal.remove();
    }

    @Pointcut(value = "execution(public * demo.annotation.*.*(..))")
    public void pointCut(){}
}
